//
//  XmlParser.m
//  lib
//

//
#import "XmlParser.h"
#import "libCommon.h"


@implementation ParserFuncTbl
@synthesize start;
@synthesize end;
@synthesize characters;


- (id)init
{
	if( self = [super init] )
	{
	}
	return self;
}


- (void)dealloc
{
	SAFE_RELEASE(start)
	SAFE_RELEASE(end)
	SAFE_RELEASE(characters)
	[super dealloc];
}
@end


@interface XmlParser(private)
- (void)initMember;
- (void)releaseMember;
@end

@implementation XmlParser
@synthesize m_FuncTbl;


- (void)initMember
{
	m_pCurrentParser = nil;
	
	m_FuncTbl = [[NSMutableArray array] retain];
	m_FuncNo = XML_PARSE_FUNC_NO_ERROR;
}

- (id)init
{
	if( self = [super init] )
		[self initMember];
	return self;
}



- (void)releaseMember
{
	SAFE_RELEASE(m_FuncTbl)
}

- (void)dealloc
{
	[self releaseMember];
	
	[super dealloc];
}



- (BOOL)parseXml:(NSData*)data
{
	return [self parseXml:data funcNo:XML_PARSE_FUNC_NO_ERROR];
}
- (BOOL)parseXml:(NSData*)data funcNo:(u_int)func_no
{
	BOOL ret = NO;
	m_FuncNo = func_no;
	
	
	NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
	{
		
		NSXMLParser* parser = [[[NSXMLParser alloc] initWithData:data] autorelease];
		[parser setDelegate:self];
		[parser setShouldResolveExternalEntities:YES];
		
		
		@try
		{
			ret = [parser parse];

#ifdef __DEBUG__
			NSError* parseError = [parser parserError];
			if( parseError )
			{
				DebLog( @"[Erorr] parse error %@", [parseError description] );
			}
#endif // __DEBUG__
		}
		@catch ( NSException *ex )
		{
			
			DebLog(@"parse exception catch");
		}
		//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
		
		m_pCurrentParser = nil;
	}
	[pool drain];
	
	m_FuncNo = XML_PARSE_FUNC_NO_ERROR;
	return ret;
}



#pragma mark -
#pragma mark NSXMLParser delegates

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
	if( m_FuncTbl
//	   && (parser == m_pCurrentParser)
	   && (m_FuncNo != XML_PARSE_FUNC_NO_ERROR)
	   && (m_FuncNo < [m_FuncTbl count])
		)
	{
		ParserFuncTbl* tbl = [m_FuncTbl objectAtIndex:m_FuncNo];
		NSString* func_name = tbl.start;
		SEL selector = NSSelectorFromString( func_name );

		
		NSMutableDictionary* new_dict = [[NSMutableDictionary new] autorelease];
		for( NSString* key in [attributeDict allKeys] ){
			NSString* value = [attributeDict objectForKey:key];
			value = convertAmp2And(value);
			[new_dict setObject:value forKey:key];
		}

		[self performSelector:selector withObject:elementName withObject:(NSDictionary*)new_dict];
	}
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
	if( m_FuncTbl
	   && (m_FuncNo != XML_PARSE_FUNC_NO_ERROR)
	   && (m_FuncNo < [m_FuncTbl count])
	   )
	{
		ParserFuncTbl* tbl = [m_FuncTbl objectAtIndex:m_FuncNo];
		NSString* func_name = tbl.end;
		SEL selector = NSSelectorFromString( func_name );
		[self performSelector:selector withObject:elementName];
	}
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
	if( m_FuncTbl
	   && (m_FuncNo != XML_PARSE_FUNC_NO_ERROR)
	   && (m_FuncNo < [m_FuncTbl count])
	   )
	{
		ParserFuncTbl* tbl = [m_FuncTbl objectAtIndex:m_FuncNo];
		NSString* func_name = tbl.characters;
		SEL selector = NSSelectorFromString( func_name );

		
		string = convertAmp2And(string);

		[self performSelector:selector withObject:string];
	}
}
/////////////////////////////////////////////////////////////////////////

@end